package chelydra.sharelydra.client.friend;

import java.util.Hashtable;
import java.util.Map;

/**
 *
 * @author Pierre-Élie Fauché
 */
public class Friend {

	protected String login;
	protected String nickname;
	protected String firstName;
	protected String lastName;
	protected String email;
	protected String ip;
	protected Integer port;

	public Friend(String login, String nickname, String firstName, String lastName, String email, String ip, Integer port) {
		this.login = login;
		this.nickname = nickname;
		this.firstName = firstName;
		this.lastName = lastName;
		this.setEmail(email);
		this.setIp(ip);
		this.setPort(port);
	}

	public Friend(Map<String,String> map) throws Exception {
		if ( (map.get("login").length()<1)
		|| (map.get("nickname").length()<1)
		|| (map.get("firstName").length()<1)
		|| (map.get("lastName").length()<1) ) {
			throw new Exception("ERR Necessary attribute is null is the map: "+map);
		}

		Integer aPort = null;
		if (map.get("port").length()>0) {
			try {
				aPort = new Integer((String)map.get("port"));
			} catch(NumberFormatException nfe) {
				throw new Exception("ERR number format exception for the port number is the map: "+map.get("port"));
			}
		}

		this.login = map.get("login");
		this.nickname = map.get("nickname");
		this.firstName = map.get("firstName");
		this.lastName = map.get("lastName");
		this.setEmail(map.get("email"));
		this.setIp(map.get("ip"));
		this.setPort(aPort);
	}

	// <editor-fold defaultstate="collapsed" desc="Getters/Setters">
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		if (email!=null)
			this.email = email;
		else
			this.email = "";
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		if (ip!=null) {
			if (this.isIpAddress(ip))
				this.ip = ip;
			else
				this.ip = "";
		} else this.ip = "";
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public Integer getPort() {
		return port;
	}

	public void setPort(Integer port) {
		if (port!=null)
			this.port = port;
		else
			this.port = new Integer(0);
	}
	// </editor-fold>

	public Boolean isConnected() {
		if ( (this.ip.length()>0) && (this.port>0) )
			return new Boolean(true);
		return new Boolean(false);
	}

	public String getName() {
		return this.getFirstName()+" "+this.getLastName();
	}

	public static boolean isIpAddress(String ip) {
		return ip.matches("^(((2(5[0-5]|[0-4]\\d))|(1\\d\\d)|([1-9]?\\d))\\.){3}((2(5[0-5]|[0-4]\\d))|(1\\d\\d)|([1-9]?\\d))$");
	}

	public static Map<String,String> getEmptyAttributesMap() {
		Map<String,String> map = new Hashtable(7);

		map.put("login", "");
		map.put("nickname", "");
		map.put("firstName", "");
		map.put("lastName", "");
		map.put("email", "");
		map.put("ip", "");
		map.put("port", "");

		return map;
	}

	@Override
	public String toString() {
		return this.getNickname();
	}

}
